@page "/pivot-table/olap"

@using Syncfusion.Blazor.PivotView
@inherits SampleBaseComponent;
@using BlazorDemos
@using ej2_blazor_productdetails

<SampleDescription>
    <p>This sample demonstrates the rendering of a pivot table bound to an online SSAS OLAP cube as its data source.
    OLAP cube elements like dimensions, hierarchies, measures, and others can be arranged in row, column, value, and
    slicer axes to create desired views at runtime.</p>
</SampleDescription>
<ActionDescription>
     <p>In this example, users can explore all of an OLAP cube and its elements and view the resultant report in a pivot
        table or pivot chart at runtime. Grouping bar and field list options are included for exploring the data. Along
        with these, toolbar options are included for switching to the pivot chart, performing report manipulation and more:</p>
    <table>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Create new report:</code></td>
            <td>Allows user to create new reports at runtime.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Rename report:</code></td>
            <td>Allows user to change current report name dynamically through UI.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Remove report:</code></td>
            <td>Allows user to remove current report from the report collection at runtime.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Save as option:</code></td>
            <td>Allows user to save report locally in browser memory.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Report list:</code></td>
            <td>Allows user to swap between the reports within the report collection.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Pivot Table:</code></td>
            <td>Allows user to view data in cross-tabulation format.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Pivot Chart and its types:</code></td>
            <td>Allows user to view data in graphical format. The chart types include column, bar, line, area, etc. It also has options for showing and hiding legends and displaying chart series of different measures on single and multiple axes.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Show MDX query:</code></td>
            <td>View the MDX query of the current pivot table that is used to fetch the data from the cube.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Export:</code></td>
            <td>Provides options to save data in PDF, Excel, and CSV document types.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Hide subtotals and grand totals:</code></td>
            <td>Hide grand totals and subtotals based on hierarchies in rows and columns.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Conditional formatting:</code></td>
            <td>Allows user to customize cells based on certain conditions.</td>
        </tr>
        <tr>
            <td style='vertical-align: top;padding: 4px 0;'><code>Field List:</code></td>
            <td>Provides option to alter the report dynamically through UI.</td>
        </tr>
    </table>
<p>More information on the OLAP data binding can be found in this <a target='_blank' href='https://blazor.syncfusion.com/documentation/pivot-table/olap/'>documentation section</a>. You can refer to our <a target='_blank' href='https://www.syncfusion.com/blazor-components/blazor-pivot-table-olap'>Blazor OLAP Pivot Table</a> page to know more about connects multidimensional data (OLAP).</p>
</ActionDescription>

<div class="control-section">
    <div class="content-wrapper">
        <SfPivotView TValue="PivotProductDetails" @ref="@Pivot" ShowGroupingBar="true" Height="500" Width="100%" ShowFieldList=true ShowToolbar=true Toolbar="@Olap" AllowConditionalFormatting=true AllowPdfExport=true AllowExcelExport=true ShowTooltip=false>
            <PivotViewDisplayOption Primary="Primary.Table" View="View.Both"></PivotViewDisplayOption>
            <PivotViewDataSourceSettings TValue="PivotProductDetails" ProviderType="ProviderType.SSAS" Catalog="Adventure Works DW 2008 SE" Cube="Adventure Works" Url="https://bi.syncfusion.com/olap/msmdpump.dll" LocaleIdentifier="1033" EnableSorting="true">
                <PivotViewColumns>
                    <PivotViewColumn Name="[Product].[Product Categories]" Caption="Product Category"></PivotViewColumn>
                    <PivotViewColumn Name="[Measures]" Caption="Measure"></PivotViewColumn>
                </PivotViewColumns>
                <PivotViewRows>
                    <PivotViewRow Name="[Customer].[Customer Geography]" Caption="Customer Geography"></PivotViewRow>
                </PivotViewRows>
                <PivotViewValues>
                    <PivotViewValue Name="[Measures].[Customer Count]" Caption="Customer Count"></PivotViewValue>
                    <PivotViewValue Name="[Measures].[Internet Sales Amount]" Caption="Internet Sales"></PivotViewValue>
                </PivotViewValues>
                <PivotViewFilters>
                    <PivotViewFilter Name="[Date].[Fiscal]" Caption="Date Fiscal"></PivotViewFilter>
                </PivotViewFilters>
                <PivotViewFilterSettings>
                    <PivotViewFilterSetting Name="[Date].[Fiscal]" Items="@Items" LevelCount="3"></PivotViewFilterSetting>
                </PivotViewFilterSettings>
            </PivotViewDataSourceSettings>
            <PivotViewGridSettings ColumnWidth="120"></PivotViewGridSettings>
            <PivotChartSettings Title="Sales Analysis">
                <PivotChartPrimaryYAxis>
                    <PivotChartPrimaryYAxisBorder Width="0"></PivotChartPrimaryYAxisBorder>
                </PivotChartPrimaryYAxis>
            </PivotChartSettings>
            <PivotViewEvents TValue="PivotProductDetails" RenameReport="RenameReport" RemoveReport="RemoveReport" SaveReport="SaveReport" LoadReport="LoadReport" FetchReport="FetchReport"></PivotViewEvents>
        </SfPivotView>
    </div>
</div>


<style>
    .e-pivotview {
        min-height: 200px;
    }

    html, body {
        height: 100%;
    }
</style>

@code{
    SfPivotView<PivotProductDetails> Pivot;
    public List<ToolbarItems> Olap = new List<ToolbarItems> {
        ToolbarItems.New,
        ToolbarItems.Save,
        ToolbarItems.SaveAs,
        ToolbarItems.Rename,
        ToolbarItems.Remove,
        ToolbarItems.Load,
        ToolbarItems.Grid,
        ToolbarItems.Chart,
        ToolbarItems.MDX,
        ToolbarItems.Export,
        ToolbarItems.SubTotal,
        ToolbarItems.GrandTotal,
        ToolbarItems.ConditionalFormatting,
        ToolbarItems.FieldList
    };
    public string[] Items = new string[] { "[Date].[Fiscal].[Fiscal Quarter].&[2002]&[4]", "[Date].[Fiscal].[Fiscal Year].&[2005]" };
    public string InitReport { get; set; }
    public string[] Name { get; set; }
    public List<string> Report = new List<string>();
    public List<string> ReportName = new List<string>();
    public void SaveReport(SaveReportArgs args)
    {
        int i = 0;
        bool IsSaved = false;
        for (i = 0; i < this.ReportName.Count; i++)
        {
            if (this.ReportName[i] == args.ReportName)
            {
                this.Report[i] = args.Report;
                IsSaved = true;
            }
        }
        if (args.Report != null && !(IsSaved))
        {
            this.Report.Add(args.Report);
            this.ReportName.Add(args.ReportName);
        }
    }
    public void FetchReport(FetchReportArgs args)
    {
        args.ReportName = ReportName.ToArray();

    }
    public async Task LoadReport(LoadReportArgs args)
    {
        int i = 0;
        int j = 0;
        for (i = 0; i < ReportName.Count; i++)
        {
            if (ReportName[i] == args.ReportName)
            {
                j = i;
            }
        }
        await this.Pivot.LoadPersistDataAsync(Report[j]);
    }
    public async Task RemoveReport(RemoveReportArgs args)
    {
        int i = 0;
        for (i = 0; i < ReportName.Count; i++)
        {
            if (ReportName[i] == args.ReportName)
            {
                ReportName.RemoveAt(i);
                Report.RemoveAt(i);
                break;
            }
        }
        if (Report.Count > 0)
        {
            await this.Pivot.LoadPersistDataAsync(Report[0]);
        }
    }
    public void RenameReport(RenameReportArgs args)
    {
        if (args.IsReportExists) {
            int j = 0;
            for (j = 0; j < ReportName.Count; j++)
            {
                if (ReportName[j] == args.Rename)
                {
                    ReportName.RemoveAt(j);
                    Report.RemoveAt(j);
                    break;
                }
            }
        }
        int i = 0;
        for (i = 0; i <= (ReportName.Count - 1); i++)
        {
            if (ReportName[i] == args.ReportName)
            {
                ReportName.RemoveAt(i);
                ReportName.Add(args.Rename);
            }
        }
    }
}